/*
 * Copyright (c) 2024 Black Sesame Technologies
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "dal_rticonnextdds/utils/EntityMappingManager.hpp"

void EntityMappingAddDataReader(DDS_DataReader* key, dal_datareader_t* value)
{
    std::lock_guard<std::mutex> lock(datareaderMtx);
    datareaderMap[key] = value;
}

void EntityMappingRemoveDataReader(DDS_DataReader* key)
{
    std::lock_guard<std::mutex> lock(datareaderMtx);
    datareaderMap.erase(key);
}

void EntityMappingUpdateDataReader(DDS_DataReader* key, dal_datareader_t* value)
{
    std::lock_guard<std::mutex> lock(datareaderMtx);
    datareaderMap[key] = value;
}

dal_datareader_t* EntityMappingGetDataReader(DDS_DataReader* key)
{
    std::lock_guard<std::mutex> lock(datareaderMtx);
    auto it = datareaderMap.find(key);
    if (it != datareaderMap.end()) {
        return it->second;
    }
    return nullptr;
}

void EntityMappingAddDataWriter(DDS_DataWriter* key, dal_datawriter_t* value)
{
    std::lock_guard<std::mutex> lock(datawriterMtx);
    datawriterMap[key] = value;
}

void EntityMappingRemoveDataWriter(DDS_DataWriter* key)
{
    std::lock_guard<std::mutex> lock(datawriterMtx);
    datawriterMap.erase(key);
}

void EntityMappingUpdateDataWriter(DDS_DataWriter* key, dal_datawriter_t* value)
{
    std::lock_guard<std::mutex> lock(datawriterMtx);
    datawriterMap[key] = value;
}

dal_datawriter_t* EntityMappingGetDataWriter(DDS_DataWriter* key)
{
    std::lock_guard<std::mutex> lock(datawriterMtx);
    auto it = datawriterMap.find(key);
    if (it != datawriterMap.end()) {
        return it->second;
    }
    return nullptr;
}

void EntityMappingAddPublisher(DDS_Publisher* key, dal_publisher_t* value)
{
    std::lock_guard<std::mutex> lock(publisherMtx);
    publisherMap[key] = value;
}

void EntityMappingRemovePublisher(DDS_Publisher* key)
{
    std::lock_guard<std::mutex> lock(publisherMtx);
    publisherMap.erase(key);
}

void EntityMappingUpdatePublisher(DDS_Publisher* key, dal_publisher_t* value)
{
    std::lock_guard<std::mutex> lock(publisherMtx);
    publisherMap[key] = value;
}

dal_publisher_t* EntityMappingGetPublisher(DDS_Publisher* key)
{
    std::lock_guard<std::mutex> lock(publisherMtx);
    auto it = publisherMap.find(key);
    if (it != publisherMap.end()) {
        return it->second;
    }
    return nullptr;
}

void EntityMappingAddSubscriber(DDS_Subscriber* key, dal_subscriber_t* value)
{
    std::lock_guard<std::mutex> lock(subscriberMtx);
    subscriberMap[key] = value;
}

void EntityMappingRemoveSubscriber(DDS_Subscriber* key)
{
    std::lock_guard<std::mutex> lock(subscriberMtx);
    subscriberMap.erase(key);
}

void EntityMappingUpdateSubscriber(DDS_Subscriber* key, dal_subscriber_t* value)
{
    std::lock_guard<std::mutex> lock(subscriberMtx);
    subscriberMap[key] = value;
}

dal_subscriber_t* EntityMappingGetSubscriber(DDS_Subscriber* key)
{
    std::lock_guard<std::mutex> lock(subscriberMtx);
    auto it = subscriberMap.find(key);
    if (it != subscriberMap.end()) {
        return it->second;
    }
    return nullptr;
}

void EntityMappingAddTopic(DDS_Topic* key, dal_topic_t* value)
{
    std::lock_guard<std::mutex> lock(topicMtx);
    topicMap[key] = value;
}

void EntityMappingRemoveTopic(DDS_Topic* key)
{
    std::lock_guard<std::mutex> lock(topicMtx);
    topicMap.erase(key);
}

void EntityMappingUpdateTopic(DDS_Topic* key, dal_topic_t* value)
{
    std::lock_guard<std::mutex> lock(topicMtx);
    topicMap[key] = value;
}

dal_topic_t* EntityMappingGetTopic(DDS_Topic* key)
{
    std::lock_guard<std::mutex> lock(topicMtx);
    auto it = topicMap.find(key);
    if (it != topicMap.end()) {
        return it->second;
    }
    return nullptr;
}

void EntityMappingAddDomainParticipant(DDS_DomainParticipant* key,
                                                dal_domain_participant_t* value)
{
    std::lock_guard<std::mutex> lock(domainParticipantMtx);
    domainParticipantMap[key] = value;
}

void EntityMappingRemoveDomainParticipant(DDS_DomainParticipant* key)
{
    std::lock_guard<std::mutex> lock(domainParticipantMtx);
    domainParticipantMap.erase(key);
}

void EntityMappingUpdateDomainParticipant(DDS_DomainParticipant* key,
                                                   dal_domain_participant_t* value)
{
    std::lock_guard<std::mutex> lock(domainParticipantMtx);
    domainParticipantMap[key] = value;
}

dal_domain_participant_t* EntityMappingUpdateGetDomainParticipant(DDS_DomainParticipant* key)
{
    std::lock_guard<std::mutex> lock(domainParticipantMtx);
    auto it = domainParticipantMap.find(key);
    if (it != domainParticipantMap.end()) {
        return it->second;
    }
    return nullptr;
}